package com.cencat.common.config;

import com.baomidou.mybatisplus.annotation.DbType;
import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.BlockAttackInnerInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.OptimisticLockerInnerInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.TenantLineInnerInterceptor;
import com.cencat.common.handler.CencatTenantLineHandler;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.transaction.annotation.EnableTransactionManagement;


/**
 * 数据库配置类
 * 配置MyBatis-Plus插件和数据库相关功能
 * 
 * @author cencat
 * @since 2024-01-01
 */
@Configuration
@EnableTransactionManagement
public class DatabaseConfig {

    private static final Logger log = LoggerFactory.getLogger(DatabaseConfig.class);

    /**
     * MyBatis-Plus拦截器配置
     * 配置分页、多租户、乐观锁、防全表更新删除等插件
     * 
     * @return MybatisPlusInterceptor实例
     */
    @Bean
    public MybatisPlusInterceptor mybatisPlusInterceptor(CencatTenantLineHandler tenantLineHandler) {
        log.info("初始化MyBatis-Plus拦截器配置");
        
        MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
        
        // 1. 多租户插件（必须放在第一位）
        TenantLineInnerInterceptor tenantInterceptor = new TenantLineInnerInterceptor();
        tenantInterceptor.setTenantLineHandler(tenantLineHandler);
        interceptor.addInnerInterceptor(tenantInterceptor);
        log.info("多租户插件已配置");
        
        // 2. 分页插件
        PaginationInnerInterceptor paginationInterceptor = new PaginationInnerInterceptor();
        paginationInterceptor.setDbType(DbType.MYSQL);
        paginationInterceptor.setMaxLimit(1000L); // 设置最大分页数量
        paginationInterceptor.setOverflow(false); // 溢出总页数后是否进行处理
        interceptor.addInnerInterceptor(paginationInterceptor);
        log.info("分页插件已配置，最大分页数量: 1000");
        
        // 3. 乐观锁插件
        OptimisticLockerInnerInterceptor optimisticLockerInterceptor = new OptimisticLockerInnerInterceptor();
        interceptor.addInnerInterceptor(optimisticLockerInterceptor);
        log.info("乐观锁插件已配置");
        
        // 4. 防全表更新与删除插件
        BlockAttackInnerInterceptor blockAttackInterceptor = new BlockAttackInnerInterceptor();
        interceptor.addInnerInterceptor(blockAttackInterceptor);
        log.info("防全表更新删除插件已配置");
        
        log.info("MyBatis-Plus拦截器配置完成");
        return interceptor;
    }

    // 元数据处理器通过@Component注解自动注册，无需在此处定义Bean

    /**
     * 数据库配置信息日志
     */
    @jakarta.annotation.PostConstruct
    public void logDatabaseConfig() {
        log.info("=== 数据库配置信息 ===");
        log.info("数据库类型: MySQL");
        log.info("事务管理: 已启用");
        log.info("多租户支持: 已启用");
        log.info("分页插件: 已启用（最大1000条）");
        log.info("乐观锁: 已启用");
        log.info("防全表操作: 已启用");
        log.info("自动填充: 已启用");
        log.info("===================");
    }

    /**
     * 数据库字段常量类
     */
    public static class DatabaseFields {
        
        /** 主键字段 */
        public static final String ID = "id";
        
        /** 租户ID字段 */
        public static final String TENANT_ID = "tenant_id";
        
        /** 创建时间字段 */
        public static final String CREATE_TIME = "create_time";
        
        /** 更新时间字段 */
        public static final String UPDATE_TIME = "update_time";
        
        /** 创建人字段 */
        public static final String CREATE_BY = "create_by";
        
        /** 更新人字段 */
        public static final String UPDATE_BY = "update_by";
        
        /** 逻辑删除字段 */
        public static final String DELETED = "deleted";
        
        /** 版本号字段（乐观锁） */
        public static final String VERSION = "version";
        
        /** 状态字段 */
        public static final String STATUS = "status";
        
        /** 排序字段 */
        public static final String SORT_ORDER = "sort_order";
        
        /** 备注字段 */
        public static final String REMARK = "remark";
    }

    /**
     * 数据库表名常量类
     */
    public static class TableNames {
        
        /** 用户表 */
        public static final String SYS_USER = "sys_user";
        
        /** 角色表 */
        public static final String SYS_ROLE = "sys_role";
        
        /** 权限表 */
        public static final String SYS_PERMISSION = "sys_permission";
        
        /** 用户角色关联表 */
        public static final String SYS_USER_ROLE = "sys_user_role";
        
        /** 角色权限关联表 */
        public static final String SYS_ROLE_PERMISSION = "sys_role_permission";
        
        /** 租户表 */
        public static final String SYS_TENANT = "sys_tenant";
        
        /** 字典类型表 */
        public static final String SYS_DICT_TYPE = "sys_dict_type";
        
        /** 字典数据表 */
        public static final String SYS_DICT_DATA = "sys_dict_data";
        
        /** 配置表 */
        public static final String SYS_CONFIG = "sys_config";
        
        /** 操作日志表 */
        public static final String SYS_OPERATION_LOG = "sys_operation_log";
        
        /** 登录日志表 */
        public static final String SYS_LOGIN_LOG = "sys_login_log";
        
        /** 商家表 */
        public static final String BIZ_MERCHANT = "biz_merchant";
        
        /** 司机表 */
        public static final String BIZ_DRIVER = "biz_driver";
        
        /** 订单表 */
        public static final String BIZ_ORDER = "biz_order";
        
        /** 线路表 */
        public static final String BIZ_ROUTE = "biz_route";
        
        /** 车辆表 */
        public static final String BIZ_VEHICLE = "biz_vehicle";
    }

    /**
     * 数据库索引常量类
     */
    public static class IndexNames {
        
        /** 租户ID索引 */
        public static final String IDX_TENANT_ID = "idx_tenant_id";
        
        /** 创建时间索引 */
        public static final String IDX_CREATE_TIME = "idx_create_time";
        
        /** 状态索引 */
        public static final String IDX_STATUS = "idx_status";
        
        /** 用户名索引 */
        public static final String IDX_USERNAME = "idx_username";
        
        /** 手机号索引 */
        public static final String IDX_PHONE = "idx_phone";
        
        /** 邮箱索引 */
        public static final String IDX_EMAIL = "idx_email";
        
        /** 组合索引：租户ID + 状态 */
        public static final String IDX_TENANT_STATUS = "idx_tenant_status";
        
        /** 组合索引：租户ID + 创建时间 */
        public static final String IDX_TENANT_CREATE_TIME = "idx_tenant_create_time";
    }
}